package com.example.shop.service.impl;

import com.example.shop.domain.Agreement;
import com.example.shop.domain.Goods;
import com.example.shop.domain.User;
import com.example.shop.mapper.AgreementMapper;
import com.example.shop.service.AgreementService;
import com.example.shop.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @projectName: travel
 * @package: com.example.travel.service.impl
 * @className: UserService
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/11 9:07
 * @version: 1.0
 */

@Service
public class AgreementServiceImpl implements AgreementService {

    @Autowired
    private AgreementMapper agreementMapper;


    @Override
    public List<Agreement> selectList(String name) {
        return agreementMapper.selectListByName(name);
    }


    @Override
    public void add(Agreement agreement) {
        Map<String, Object> map = ThreadLocalUtil.get();
        agreement.setUserOut((Integer) map.get("id"));

//        Outlay outlay1 =new Outlay();
//        outlay1.setAmount(outlay.getAmount());
//        outlay1.setRemark(outlay.getRemark());
//        outlay1.setChannelId(outlay.getChannelId());
//        outlay1.setCategoryId(outlay.getCategoryId());
//        System.out.println(outlay.getCategoryId());

        agreementMapper.insert(agreement);
    }


    @Override
    public Agreement selectById(Agreement agreement) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        agreement.setUserIn(id);
        return agreementMapper.selectById(agreement.getId());
    }


    @Override
    public int delete(Agreement agreement) {
        return agreementMapper.deleteById(agreement);
    }

    @Override
    public void update(Agreement agreement) {
        agreementMapper.updateById(agreement);
    }

    @Override
    public int borrow(Goods goods) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Agreement agreement = new Agreement();
        agreement.setGoodsId(goods.getId());
        agreement.setUserOut(goods.getUserId());
        agreement.setUserIn(id);
        agreement.setStatus(0);
        agreementMapper.insertRTID(agreement);
        return agreement.getId();

    }



    @Override
    public List<Agreement> selectListIn() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        return agreementMapper.selectListIn(id);
    }

    @Override
    public Agreement getAgreementInf(Goods goods) {

        return agreementMapper.selectAgreementInf(goods);
    }

    @Override
    public void lend(Goods goods) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Agreement agreement = new Agreement();
        agreement.setUserOut(id);
        agreement.setStatus(1);
        agreement.setGoodsId(goods.getId());
        agreementMapper.updateByAgreement(agreement);
    }

    @Override
    public void takeBack(Goods goods) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Agreement agreement = new Agreement();
        agreement.setUserOut(id);
        agreement.setStatus(3);
        agreement.setGoodsId(goods.getId());
        agreementMapper.updateByAgreement(agreement);
    }

    @Override
    public void returnGoods(int id) {
//        System.out.println(goods);
//        Map<String, Object> map = ThreadLocalUtil.get();
//        Integer id = (Integer) map.get("id");
//        Agreement agreement = new Agreement();
//        agreement.setUserOut(id);
//        agreement.setStatus(2);
//        agreement.setGoodsId(goods.getId());
//        System.out.println(agreement);
        agreementMapper.returnGoodsById(id);
    }

    @Override
    public User getInInf(Goods goods) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Agreement agreement = new Agreement();
        agreement.setUserOut(id);
        agreement.setGoodsId(goods.getId());

        return agreementMapper.selectUserByAgreement(agreement);
    }


}
